package com.yln.configuration;

import com.yln.watcher.client.ZkClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Slf4j
@Configuration
public class ZkConfig {

    @Value("${zk.clientId}")
    private String zkClientId;

    @Value("${zk.serverConnection}")
    private String zkServerConnection;

    @Bean(destroyMethod = "stop")
    public ZkClient zkClient() {
        ZkClient zkClient = new ZkClient(zkServerConnection, zkClientId);
        try {
            zkClient.start();
            zkClient.initContext();
            return zkClient;
        } catch (IOException e) {
            log.error("start zk connection error.", e);
            throw new RuntimeException("zkClient init error.", e);
        }
    }
}
